package com.zee.example.OkUtils;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.google.gson.Gson;
import com.zee.example.BeanTotal;
import com.zee.example.BuildConfig;
import com.zee.http.MyOk;
import com.zee.http.bean.UploadFileParams;
import com.zee.http.request.DownAPKAndInstallCallBackListener;
import com.zee.http.request.UploadingFileCallBackListener;
import com.zee.http.request.ZCacheStringResult;
import com.zee.log.ZLog;
import com.zee.utils.UIUtils;

import java.io.File;

public class OkUtlis {

    /**
     * 上传文件
     */
    public void upLoadFile() {
        UploadFileParams params = new UploadFileParams();
//        File file=new File("/storage/emulated/0/daorong/headercamare/mypic.png");
        params.upLoadFile("filename", new File("/storage/emulated/0/daorong/headercamare/mypic.png"));
        MyOk.uploadFile("http://test.daorongwangluo.com/FileServer/Upload?t=0D2D5294A5E30777&d=1&n=122914.png").setUploadFileParams(params).execute(new UploadingFileCallBackListener() {
            @Override
            public void onStart() {
                ZLog.i("onstart()");
            }

            @Override
            public void onUploadSuccess(String data) {
                ZLog.i("上传成功" + data);
            }

            @Override
            public void onError(Exception e) {
                super.onError(e);
                ZLog.exception(e);
            }

            @Override
            public void onEnd() {
                ZLog.i("onEnd()");
            }

            @Override
            public void onUploadProgress(float progress, long currentSize, long totalSize, long networkSpeed) {
                super.onUploadProgress(progress, currentSize, totalSize, networkSpeed);
                ZLog.i("onUploadProgress" + progress);
            }
        });
    }




    private void test() {
        LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(300) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return super.sizeOf(key, value);
            }
        };
        int cacheSize = 4 * 1024 * 1024; // 4MiB
        LruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) {
            protected int sizeOf(String key, Bitmap value) {
                return value.getByteCount();
            }
        };
    }

//    public TextView getTextView(int index) {
//        TextView textView = new TextView(this);
//        textView.setText("第" + index + "个");
//        textView.setBackgroundColor(Color.parseColor("#00ff00"));
//        return textView;
//    }



    //下载文件
    private void downFileTest() {
        try {
            MyOk.downloadFile("http://test.daorongwangluo.com/game/daorong.apk").execute(new DownAPKAndInstallCallBackListener() {


                @Override
                protected String BuildConfigApplicationID() {
                    return BuildConfig.APPLICATION_ID;
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //普通的网络请求
    //http://api.daorongwangluo.com/api/show/v1/L004/a002.php
    private void request() {
        MyOk.load("http://api.daorongwangluo.com/api/show/v1/L004/a002.php?typeId=21").post().putParams("pageSize", 5).putParams("typeId", 2).execute(new ZCacheStringResult() {
            @Override
            public void onStart() {
                ZLog.i("进入onStart()->" + Thread.currentThread().getName());
            }

            @Override
            public void onSuccessAsyncThread(String data, boolean isFromCache) {

                ZLog.i("加载结束,线程名字:" + Thread.currentThread());
//                ZLog.i(isCache + "进入onSuccess()->" + data);
                Gson gson = new Gson();
                BeanTotal wanandroid = gson.fromJson(data, BeanTotal.class);
//                UIUtils.runOnMainThread(() -> {
//                    if (ZListUtils.isEmpty(wanandroid.getData())) {
//                        mXRecyclerView.setNoMore(true);
//                    }
//                });

//                dataList.addAll(wanandroid.getData());
                UIUtils.runOnMainThread(new Runnable() {
                    @Override
                    public void run() {
//                        if (curpage == 1) {
////                            mXRecyclerView.loadFinish();
//                        } else {
                        UIUtils.postDelayed(new Runnable() {
                            @Override
                            public void run() {
//                                mXRecyclerView.loadFinish();
//                                mXRecyclerView.setNoMore(true);
                            }
                        }, 8000);
                    }
//                    }
                });
//                mBaseZAdapter.notifyDataSetChanged();
//                mTestAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(boolean isCache, Exception e) {
                ZLog.i(isCache + "进入onError()->" + Thread.currentThread().getName());
            }

            @Override
            public void onEnd() {
                ZLog.i("进入onEnd()->" + Thread.currentThread().getName());
            }
        });
    }

}
